﻿using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.ViewModels;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Animation;

namespace JiveMessenger.Views
{
    /// <summary>
    /// Description for DownloadProgressWindow.
    /// </summary>
    public partial class DownloadProgressWindow : Window
    {
        public double WindowTop
        {
            get { return (double)GetValue(WindowTopProperty); }
            set { SetValue(WindowTopProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Top.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WindowTopProperty =
            DependencyProperty.Register("Top", typeof(double), typeof(DownloadProgressWindow), new PropertyMetadata(0.0, new PropertyChangedCallback(TopPropertyChanged)));


        public double WindowLeft
        {
            get { return (double)GetValue(WindowLeftProperty); }
            set { SetValue(WindowLeftProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Left.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WindowLeftProperty =
            DependencyProperty.Register("Left", typeof(double), typeof(DownloadProgressWindow), new PropertyMetadata(0.0, new PropertyChangedCallback(LeftPropertyChanged)));


        private static void TopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DownloadProgressWindow win = d as DownloadProgressWindow;
            if (e.NewValue != null)
            {
                win.Top = (double)e.NewValue-35;
            }
        }

        private static void LeftPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DownloadProgressWindow win = d as DownloadProgressWindow;
            if (e.NewValue != null)
            {
                win.Left = (double)e.NewValue-70;
            }
        }
        /// <summary>
        /// Initializes a new instance of the DownloadProgressWindow class.
        /// </summary>
        public DownloadProgressWindow(double left, double top)
        {
            InitializeComponent();
            this.Left = left - 70;
            this.Top = top -35;
            ImageLoadingGrid.Opacity = 0;
            Binding myBinding = new Binding("Left");
            myBinding.Source = ((IViewModelDownloadProgress)this.DataContext);
            myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            this.SetBinding(WindowLeftProperty, myBinding);

            myBinding = new Binding("Top");
            myBinding.Source = ((IViewModelDownloadProgress)this.DataContext);
            myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            this.SetBinding(WindowTopProperty, myBinding);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.ImageLoadingGrid.Opacity = 0;          
        }
    }
}